package com.cnoja.jmsncn.utils.file;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import com.cnoja.jmsncn.utils.file.exceptions.FileCreationException;
import com.cnoja.jmsncn.utils.file.exceptions.FileException;
import com.cnoja.jmsncn.utils.io.IOUtils;
import com.cnoja.jmsncn.utils.text.StringUtils;

public class FileUtils {

	public static URL getExecuteRuntimeRoot(Object o) {
		return o.getClass().getClassLoader().getResource(".");

	}

	public static String getExecuteRuntimeRootInString(Object o) {
		String s = o.getClass().getClassLoader().getResource(".").getPath();

		while (s.startsWith("/")) {
			s = s.substring(1);
		}
		return s;
	}

	public String getFileContent(File f, String charSet) throws IOException,
			FileException {
		nullChecker(f);
		fileChecker(f);
		if (!f.exists()) {
			throw new FileNotFoundException(f + " is does not exist!");
		}

		IOUtils utils = new IOUtils();
		return utils.getAsString(new FileInputStream(f), charSet);

	}

	public void writeFile(File f, InputStream stream) throws IOException,
			FileException {
		nullChecker(f);
		fileChecker(f);
		if (!f.exists()) {
			if (!f.createNewFile()) {
				throw new FileCreationException(
						"The file creation encountered an error");
			}
			;
		}
		OutputStream outputStream = new BufferedOutputStream(
				new FileOutputStream(f));
		IOUtils utils = new IOUtils();
		utils.copy(stream, outputStream, true, true);
	}

	private void fileChecker(File f) {
		if (f.isFile()) {
			throw new IllegalArgumentException("The parameter must be a file");
		}
	}

	private void nullChecker(File f) {
		if (f == null) {
			throw new IllegalArgumentException(
					"The parameter should not be null");
		}
	}

	public static FileInputStream openInputStream(String path)
			throws FileNotFoundException {
		File file = new File(path);
		if (!file.isFile()) {
			throw new IllegalArgumentException(
					"The parameter refers to an invalid file!");
		}
		return new FileInputStream(file);

	}

	public static void delete(String path) {
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
	}

	public FileOutputStream openOutputStream(String path, boolean createNew)
			throws IOException {
		if (StringUtils.isNullOrEmpty(path)) {
			throw new IllegalArgumentException(
					"The parameter can not be null or empty");
		}
		File file = new File(path);
		if (file.exists()&&file.isFile()) {
			return new FileOutputStream(file);
		} else if (createNew) {
			file.createNewFile();
			return new FileOutputStream(file);
		}
		
		

		throw new FileNotFoundException("The file \"" + path
				+ "\" does not exists");
	}

	public static String getExecuteRuntimeBasedPath(Object o, String string) {
		if (StringUtils.isNullOrEmpty(string)) {
			return getExecuteRuntimeRootInString(o);
		} else {
			return getExecuteRuntimeRootInString(o)
					+ (string.startsWith("/") ? string.substring(1) : string);
		}
	}

}
